package com.key.web_admin.controller;

import com.key.common.base.RespResult;
import com.key.modules.dto.VehicleAddDTO;
import com.key.modules.dto.VehicleEditDTO;
import com.key.modules.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wei
 */
@RestController
@RequestMapping("/vehicle")
public class VehicleController {

    @Autowired
    VehicleService vehicleService;

    /**
     * 查询车辆表（支持条件查询）
     * @author wei
     * @param currentPage 当前页码数
     * @param pageSize 每页显示记录数
     * @param vNumber 车牌号（条件查询时的可选参数）
     * @param vType 类型（条件查询时的可选参数）
     * @param cName 所属承运商（条件查询时的可选参数）
     * @return
     */
    @GetMapping("/findVehicleList")
    public RespResult findVehicleList(Integer currentPage, Integer pageSize,
                                      String vNumber, String vType, String cName) {
        RespResult respResult;
        try{
            respResult = vehicleService.findVehicleList(currentPage, pageSize, vNumber, vType, cName);
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }

        return respResult;
    }

    /**
     * 添加车辆
     * @author wei
     * @param vehicleAddDTO 新增车辆时需要填写的参数
     * @return
     */
    @PostMapping("/addVehicle")
    public RespResult addVehicle(@RequestBody VehicleAddDTO vehicleAddDTO) {
        RespResult respResult;
        try{
            boolean result = vehicleService.addVehicle(vehicleAddDTO);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

    /**
     * 删除车辆
     * @author wei
     * @param vId 车辆ID
     * @return
     */
    @PostMapping("/removeVehicle")
    public RespResult removeVehicle(Integer vId) {
        RespResult respResult;
        try{
            boolean result = vehicleService.removeVehicle(vId);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

    /**
     * 修改车辆
     * @author wei
     * @param vehicleEditDTO 修改车辆时需要填写的参数
     * @return
     */
    @PostMapping("/editVehicle")
    public RespResult editVehicle(@RequestBody VehicleEditDTO vehicleEditDTO) {
        RespResult respResult;
        try{
            boolean result = vehicleService.editVehicle(vehicleEditDTO);
            if(result){
                respResult = RespResult.success();
            }else{
                respResult = RespResult.error("修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            respResult = RespResult.error("后台出错");
        }
        return respResult;
    }

}
